package com.tiles.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.tiles.tools.DesUtil;
import com.tiles.entity.TbStudent;
import com.tiles.service.StudentService;
import com.tiles.service.TilesUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TilesUserService tilesUserService;

    //   测试tiles中body页面数据是否正常
    @RequestMapping("/body")
    @ResponseBody
    public List<TbStudent>  test1(HttpServletRequest request,Model model){

        Jedis jedis = new Jedis();
        //判断redis中是否有 这个key
        if (jedis.exists("stuList")) {
            String student = jedis.get("stuList");
            //            将stuList取出并转为List 类型
            List<TbStudent> students = JSON.parseArray(student, TbStudent.class);
            //    关闭redis连接
            jedis.close();

            return students;
        }
            Example abc = new Example(TbStudent.class);
            Example.Criteria criteria = abc.createCriteria();
            criteria.toString();
            abc.orderBy("salary").desc();
            List<TbStudent> students = studentService.selectStudentAll(abc);
             //将students转Json格式存入redis
             jedis.set("stuList", JSON.toJSONString(students));

            jedis.close();

        return students;
    }
    @GetMapping("/home")
    public String  test2(HttpServletRequest request,Model model) throws Exception {
        //分页
        PageHelper.startPage(1, 4);
        //按照学生salary工资   降序排列
        Example abc = new Example(TbStudent.class);
        Example.Criteria criteria = abc.createCriteria();
        criteria.toString();
        abc.orderBy("salary").desc();
        List<TbStudent> students=studentService.selectStudentAll(abc);
        System.out.println("==============================>");
        System.out.println(students);
        model.addAttribute(students);
        request.setAttribute("students",students);

        //    获取cookie中的信息
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            for (Cookie cookie : cookies) {
                //如果cookie中有学生名字的信息就显示登陆
                if (tilesUserService.selectByUserName(cookie.getValue())>0){
                    //给学生名字解密
                    String name = DesUtil.desDecry(cookie.getValue(),"tiles");
                    model.addAttribute("username", name);
                    return "home1";
                }
            }
        }

        return "home";
    }


}
